package com.ljh.bs.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ljh.bs.service.BookInfoService;

@Controller
@RequestMapping(value="/bookInfo")
public class BookInfoController {
	
	@Autowired
	BookInfoService bookInfoService;
	
	@RequestMapping(value="/findAll")
	public Object findAll(Map<String,Object> map1,@RequestParam(value="indexPage",defaultValue = "1") Integer indexPage,
			//Map<String,Object> map1 相当于HttpServletRequest request
			//map1.put("","")相当于request.setAttribute("","");
			@RequestParam(value="typeName",defaultValue="0") Integer typeName,
			@RequestParam(value="likeName",defaultValue="") String likeName,@RequestParam(value="isBorrow",defaultValue="-1") Integer isBorrow){
		//每一页显示的数据
		int indexSize = 5;
		
		if(likeName != null){
			try {
				likeName = new String (likeName.getBytes("iso-8859-1"),"utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		
		//根据用户所在当前页  求出从第几条数据开始检索数据
		int index = (indexPage - 1)*indexSize;
		 
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("book_name", likeName);
		map.put("id", typeName);
		map.put("is_borrow", isBorrow);
		map.put("index", index);
		map.put("indexSize", indexSize);
		
		map.put("book_type", typeName);
		
		List<Map<String, Object>> list =bookInfoService.findAll(map);
		
		//保存数据 页面显示
		
		map1.put("list", list);
		map1.put("indexPage", indexPage);
		//总条数
		map1.put("indexCount", bookInfoService.count(map));
		//总页数
		map1.put("pageCount", bookInfoService.pageCount(map));
		
		//回现数据
		map1.put("typeName", typeName);
		map1.put("likeName", likeName);
		map1.put("isBorrow", isBorrow);
		
		return "index";
	}
}
